package com.botann.projects.service;

import com.botann.projects.base.BaseService;
import com.botann.projects.model.Cmd;
import com.botann.projects.model.Projects;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * projects
 * ProjectsService
 * Created by 刘山国 on 2020/5/12.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
@Service
public class ProjectsService extends BaseService implements ProjectsServiceIntf {
    @Override
    public Class loggerClass() {
        return ProjectsService.class;
    }

    @Override
    public Projects addUpdateProject(String json) {
        Projects projects = jsonToModel(json,Projects.class);
        if (projects.getId() <= 0) {
            projects.setCreateTime(new Date());
        }
        sessionSaveOrUpdate(projects);
        return projects;
    }

    @Override
    public Object loadProjects(Integer pageIndex, Integer pageSize) {
        return sessionQuery(Projects.class,pageIndex,pageSize);
    }

    @Override
    public void delete(String json) {
        List<Integer> ids = jsonToListModel(json,Integer.class);
        for (Integer id : ids) {
            sessionDelete(Projects.class,id);
        }
    }

    @Override
    public void addUpdateCmd(String json) {
        Cmd cmd = jsonToModel(json,Cmd.class);
        if (cmd.getProjectId() <= 0) throw sgErrorRuntimeException("请添加项目所属");
        cmd.setCreateTime(new Date());
        sessionSaveOrUpdate(cmd);
    }

    @Override
    public Object loadCmds(Integer projectId) {
        return sessionQuery(Cmd.class,conditions("projectId=","del="),projectId,false);
    }

    @Override
    public void deleteCmds(String json) {
        List<Integer> ids = jsonToListModel(json,Integer.class);
        for (Integer id : ids) {
            sessionDelete(Cmd.class,id);
        }
    }

    @Override
    public Cmd loadCmd(Integer id) {
        return sessionGetNN(Cmd.class,id);
    }
}
